package tableMaker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;

public class TableTools {
	
	static void initializeSimpleFile(FileWriter file, int dim) throws IOException{
		file.append("Instance ");
		file.append("\t k=1");
		file.append("\t k=0");
		file.append("\t k="+Integer.toString(dim));
		file.append("\t k=-"+Integer.toString(dim));
		file.append("\t Affine DR");
		file.append("\t Bound");
		//String aux = new String();
		//for(int i = 0; i < dim; i++)
		//	aux += Integer.toString(i) + "\t";
		
		file.append( "\n");
	}

	static void initializeLatexFile(FileWriter file, int dim) throws IOException{
		file.append("\\begin{tabular}{|c|c|c|c|c|} \n");
		file.append("\\hline \n Instance");
		file.append(" & Partly Adj(abs. value)");
		file.append(" & Non Adj(rel. value)");
		file.append(" & Fully Adj(rel. value)");
		file.append(" & Dec. Rules(rel. value)");
		file.append("\\hline \\\\ \n");
	}
	
	static int readResultFile(BufferedReader input, String[] names, double[][] solutions, double[][] times) throws NumberFormatException, IOException{
		String read;
		int d;
		int k;
		int g;
		int vert;
		double time;
		double sol;
		int index = 0;
		int ind = 0;
		int lastindex = 0;
		int gamma;
	
		while((read = input.readLine()) != null ){
			d = Integer.parseInt(input.readLine());
			k = Integer.parseInt(input.readLine());
			gamma = Integer.parseInt(input.readLine());
			vert = Integer.parseInt(input.readLine());
			sol = Double.parseDouble(input.readLine());
			time = Double.parseDouble(input.readLine());
			boolean test = true;
		
			ind = 0;
			while(ind < lastindex && names[ind] != null){
				if(names[ind].equals(read)){
					index = ind;
					test = false;
					break;
				}
				ind++;
			}
			
			if(test)index = lastindex++;
			names[index] = read;
			solutions[index][k+20] = sol;
			times[index][k+20] = time;
		
		}
		return lastindex;
	}

	public static void initializeSimpleFileBD(FileWriter file, int dim) throws IOException {
		file.append("Instance \t Bound1 \t Bound2 \n");
	}

	public static void initializeLatexFileBD(FileWriter file, int dim) throws IOException {
		file.append("\\begin{tabular}{|c|c|c|} \n");
		file.append("Instance & Bound1 & Bound2 \\\\ \n");
	}

	public static int readResultFileBD(BufferedReader input, String[] names, double[] sol1, double[] sol2) throws NumberFormatException, IOException {
		String read;
		int d;
		double s2;
		double s1;
		int lastindex = 0;
		int gamma;
	
		while((read = input.readLine()) != null ){
			d = Integer.parseInt(input.readLine());
			gamma = Integer.parseInt(input.readLine());
			s1 = Double.parseDouble(input.readLine());
			s2 = Double.parseDouble(input.readLine());
		
			names[lastindex] = read;
		
			sol1[lastindex] = s1;
			sol2[lastindex++] = s2;
		
		}
		return lastindex;
	}


	public static void readResultFileDR(BufferedReader input,
			double[][][] solutions, HashMap<String, Integer> instances,
			HashMap<Integer, Integer> kvalue, HashMap<String, Integer> set, int continst) throws NumberFormatException, IOException {
		String read;
		String setname;
		int d;
		int k;
		int g;
		int vert;
		double time;
		double sol;
		int index1 = 0;
		int index2 = 0;
		int index3 = 0;
		int gamma;
	
		while((read = input.readLine()) != null ){
			d = Integer.parseInt(input.readLine());
			setname = input.readLine();
			k = Integer.parseInt(input.readLine());
			gamma = Integer.parseInt(input.readLine());
			vert = Integer.parseInt(input.readLine());
			sol = Double.parseDouble(input.readLine());
			time = Double.parseDouble(input.readLine());

			index1 = instances.get(read);
			index2 = set.get(setname);
			solutions[index1][index2][4] = sol;
		
		}
	}

	public static void readResultFile(BufferedReader input,
			double[][][] solutions, HashMap<String, Integer> instances,
			HashMap<Integer, Integer> kvalue, HashMap<String, Integer> set, int continst) throws NumberFormatException, IOException {
		String read;
		String setname;
		int d;
		int k;
		int g;
		int vert;
		double time;
		double sol;
		int index1 = 0;
		int index2 = 0;
		int index3 = 0;
		int gamma;
	
		while((read = input.readLine()) != null ){
			d = Integer.parseInt(input.readLine());
			setname = input.readLine();
			k = Integer.parseInt(input.readLine());
			gamma = Integer.parseInt(input.readLine());
			vert = Integer.parseInt(input.readLine());
			sol = Double.parseDouble(input.readLine());
			time = Double.parseDouble(input.readLine());

			index1 = instances.get(read);
			index2 = set.get(setname);
			index3 = kvalue.get(k);
			solutions[index1][index2][index3] = sol;
		
		}
	}
}
